package com.art.base.commonui;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.text.style.ClickableSpan;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class ViewHolder {
	private final SparseArray<View> mViews;
	private View mConvertView;
	private int position;

	private ViewHolder(Context context, ViewGroup parent, int layoutId,
			int position) {
		this.position = position;
		this.mViews = new SparseArray<View>();
		mConvertView = LayoutInflater.from(context).inflate(layoutId, parent,
				false);
		// setTag
		mConvertView.setTag(this);

	}

	/**
	 * 拿到一个ViewHolder对象
	 * 
	 * @param context
	 * @param convertView
	 * @param parent
	 * @param layoutId
	 * @param position
	 * @return
	 */
	public static ViewHolder get(Context context, View convertView,
			ViewGroup parent, int layoutId, int position) {

		if (convertView == null) {
			return new ViewHolder(context, parent, layoutId, position);
		} else {
			ViewHolder holder = (ViewHolder) convertView.getTag();
			// 复用position
			holder.position = position;
			return (ViewHolder) convertView.getTag();
		}

	}

	/**
	 * 通过控件的Id获取对于的控件，如果没有则加入views
	 * 
	 * @param viewId
	 * @return
	 */
	public <T extends View> T getView(int viewId) {

		View view = mViews.get(viewId);
		if (view == null) {
			view = mConvertView.findViewById(viewId);
			mViews.put(viewId, view);
		}
		return (T) view;
	}

	public View getConvertView() {
		return mConvertView;
	}

	/***
	 * 设置item中textView的文本
	 * 
	 * @param viewId
	 * @param text
	 * @return
	 */
	public ViewHolder setText(int viewId, String text) {
		TextView tv = getView(viewId);
		tv.setText(text);
		return this;

	}

	/***
	 * 设置item中的ImageView的url
	 * 
	 * @param viewId
	 * @param url
	 * @return
	 */
	public ViewHolder setImageUrl(int viewId, String url) {
		ImageView iv = getView(viewId);
//		ImageLoader_.getInstance().addTask(url, iv);
		return this;

	}

	/***
	 * 设置item中的ImageView的drawable
	 * 
	 * @param viewId
	 * @param url
	 * @return
	 */
	public ViewHolder setImgDrawable(int viewId, int resId) {
		ImageView iv = getView(viewId);
		iv.setImageResource(resId);
		return this;

	}

	public ViewHolder setImgDrawable(int viewId, Drawable drawable) {
		ImageView iv = getView(viewId);
		iv.setImageDrawable(drawable);
		return this;

	}

	public ViewHolder setImgDrawable(int viewId, byte[] b) {
		ImageView iv = getView(viewId);
//		Bitmap bp = UtilTool.bytes2Bimap(b);
//		iv.setImageBitmap(bp);
		return this;

	}

	public ViewHolder setSelected(int viewId, boolean bl) {
		View view = getView(viewId);
		view.setSelected(bl);
		return this;

	}

	/**
	 * 设置能否显示
	 * 
	 * @param viewId
	 * @param Visibility
	 * @return
	 */
	public ViewHolder setVisibility(int viewId, int Visibility) {
		View v = getView(viewId);
		v.setVisibility(Visibility);
		return this;
	}

	/**
	 * 按钮的点击
	 * 
	 * @param viewId
	 * @param listener
	 * @return
	 */
	public ViewHolder setBtnClick(int viewId, OnClickListener listener) {
		Button btn = getView(viewId);
		btn.setOnClickListener(listener);
		return this;
	}

	/**
	 * 按钮的文本
	 * 
	 * @param viewId
	 * @param msg
	 * @return
	 */
	public ViewHolder setBtnText(int viewId, String msg) {
		Button btn = getView(viewId);
		btn.setText(msg);
		return this;
	}

	/**
	 * 
	 * @param viewId
	 * @param resId
	 * @return
	 */
	public ViewHolder setBtnBackGround(int viewId, int resId) {
		Button btn = getView(viewId);
		Drawable drawable = btn.getResources().getDrawable(resId);
		drawable.setBounds(0, 0, drawable.getMinimumWidth(),
				drawable.getMinimumHeight());
		btn.setCompoundDrawables(drawable, null, null, null);
		return this;
	}
}